package com.micosco.taskremind.data;

import android.util.Log;

import androidx.room.TypeConverter;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class RemindType {
    //提醒方式枚举，内容与res/values/arrays.xml中的remind_type_array保持一致
    //不提醒
    public static final RemindType NONE = new RemindType("none");
    //每天都提醒一次
    public static final RemindType EVERY_DAY = new RemindType("everyday");
    //截止前的最后一个周末提醒2次
    public static final RemindType WEEKEND_BEFORE_DEADLINE = new RemindType("weekend before deadline");
    //截止前一天提醒一次
    public static final RemindType DAY_BEFORE_DEADLINE = new RemindType("day before deadline");
    //指定每周星期几提醒一次
    public static final RemindType DAY_OF_THE_WEEK = new RemindType("day of the week");

    public static final List<RemindType> ALL_TYPE = Collections.unmodifiableList(Arrays.asList
            (NONE, EVERY_DAY, WEEKEND_BEFORE_DEADLINE, DAY_BEFORE_DEADLINE, DAY_OF_THE_WEEK));

    private static final String LOG_TAG = "data.RemindType";

    private String description;

    private RemindType(String type) {
        description = type;
    }

    public String getDescription() {
        return description;
    }

    @TypeConverter
    public static RemindType revertRemindType(String type) {
        if (type == null) {
            Log.e(LOG_TAG, "RemindType revertRemindType() receive a null.");
        }
        if (type != null) {
            for (RemindType temp : ALL_TYPE) {
                if (type.equals(temp.description)) {
                    return temp;
                }
            }
        }
        return NONE;
    }

    @TypeConverter
    public static String convertRemindType(RemindType type) {
        if (type == null) {
            Log.e(LOG_TAG, "RemindType revertRemindType() receive a null.");
            return "none";
        } else {
            return type.getDescription();
        }
    }

    @Override
    public String toString() {
        return description;
    }

    public static int getPosition(RemindType type) {
        for (int i = 0; i < ALL_TYPE.size(); i++) {
            if (type == ALL_TYPE.get(i)) {
                return i;
            }
        }
        return 0;
    }
}
